﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Korbit;
using Korbit.Data;
using Korbit.Constant;

namespace Korbit.Common
{
    public class OrderExchange
    {
        KorbitAccessInfo _AccessInfo;

        public OrderExchange()
        {
            _AccessInfo = null;
        }

        public OrderExchange(KorbitAccessInfo _info)
        {
            _AccessInfo = _info;
        }

        public string OrderBuy(eOrderType eOrder, string strKRWPrice, string strBTCVolume, string strKRWAmount)
        {
            string strReturn = "";
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면
            if (_AccessInfo == null)
                return strReturn;
            if (!_AccessInfo.IsLogin())
                return strReturn;

            string strOrder = "limit";
            string strPrice = "";
            string strCoin_Amount = "";
            string strFiat_amount = "";

            if (eOrder == eOrderType.limit)
            {
                strOrder = "limit";
            }
            else
            {
                strOrder = "market";
            }

            strPrice = strKRWPrice.ToString();
            strCoin_Amount = strBTCVolume;
            strFiat_amount = strKRWAmount;

            Dictionary<string, string> dicURLParameter = _AccessInfo.GetURLParameterAccessKey();
            dicURLParameter.Add("type", strOrder);
            dicURLParameter.Add("price", strPrice);
            dicURLParameter.Add("coin_amount", strCoin_Amount);
            dicURLParameter.Add("fiat_amount", strFiat_amount);

            //접속 주소를 입력한다.
            strURL = _AccessInfo.GetBaseURL() + KorbitURL.URL_ORDER_BUY;
            strParameters = CommonFunction.ConvertURLParameter(dicURLParameter);

            strData = Communication.GetRequestData(strURL, strParameters);

            if (strData.Length == 0)
                return strReturn;

            strReturn = strData;

            return strReturn;
        }

        public string OrderSell(eOrderType eOrder, string strKRWPrice, string strBTCVolume)
        {
            string strReturn = "";
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면
            if (_AccessInfo == null)
                return strReturn;
            if (!_AccessInfo.IsLogin())
                return strReturn;

            string strOrder = "limit";
            string strPrice = strKRWPrice;
            string strCoin_Amount = strBTCVolume;

            if (eOrder == eOrderType.limit)
            {
                strOrder = "limit";             
            }
            else
            {
                strOrder = "market";
            }

            Dictionary<string, string> dicURLParameter = _AccessInfo.GetURLParameterAccessKey();
            dicURLParameter.Add("type", strOrder);
            dicURLParameter.Add("price", strPrice);
            dicURLParameter.Add("coin_amount", strCoin_Amount);

            //접속 주소를 입력한다.
            strURL = _AccessInfo.GetBaseURL() + KorbitURL.URL_ORDER_SELL;
            strParameters = CommonFunction.ConvertURLParameter(dicURLParameter);

            strData = Communication.GetRequestData(strURL, strParameters);

            if (strData.Length == 0)
                return strReturn;

            strReturn = strData;

            return strReturn;
        }
                
        public string OrderCancel(string strOrderID)
        {
            string strReturn = "";
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면
            if (_AccessInfo == null)
                return strReturn;
            if (!_AccessInfo.IsLogin())
                return strReturn;

            Dictionary<string, string> dicURLParameter = _AccessInfo.GetURLParameterAccessKey();
            dicURLParameter.Add("id", strOrderID);

            strURL = _AccessInfo.GetBaseURL() + KorbitURL.URL_ORDER_CANCEL;
            strParameters = CommonFunction.ConvertURLParameter(dicURLParameter);
            strData = Communication.GetRequestData(strURL, strParameters);

            if (strData.Length == 0)
                return strReturn;

            strReturn = strData;

            return strReturn;
        }

        public string OpenOrderList()
        {
            string strReturn = "";

            return strReturn;
        }
    }
}
